package com.lhn.bbs.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class textcipintongji {

    static String[] a;

    public static void main(String[] args) throws IOException {
//获取文件的内容
//        String msg=getFromTxt("D:/li.txt");
//测试字符窜
        String msg = "The thermometer had dropped to 18 degrees below zero, but still chose to sleep in the porch as usual. In the evening, the most familiar sight to me would be stars in the sky. Though they were a mere sprinkle of twinkling dots, yet I had become so accustomed to them that their occasional absence would bring me loneliness and ennui.";
//消除标点符号的影响
        msg = delectChar(msg);
//用空格分隔字符窜成字符数组
        String[] b = msg.split(" ");
//存储键值对的键
        a = new String[b.length];
        int c = 0;

        System.out.println(msg);
//以键值对的形势存储单个字符窜，重复的键值对，键不变，值加1；初次出现的键值对，值置为1
        Map map = new HashMap<String, Integer>();
        for (int i = 0; i < b.length; i++) {
            boolean isHas = false;
            String sub = b[i];
            for (int j = 0; j < i; j++) {
                if (sub.equals(a[j])) {
                    map.put(sub, Integer.parseInt(map.get(sub) + "") + 1);
                    isHas = true;
                    break;
                }
            }
            if (!isHas) {
                a[c++] = sub;
                map.put(sub, 1);
            }
        }

//对键值对的值用直接插入排序
        map = insertionsort(map);
//输出前十个高频词
        for (int k = 0; k < 10 && k < a.length; k++) {
            System.out.println(a[k] + "---------" + map.get(a[k]));
        }
    }

    //直接插入排序  从大到小
    public static Map insertionsort(Map map) {
        for (int i = 1; i < map.size(); i++) {
            int item = Integer.parseInt(map.get(a[i]) + "");
            String bb = a[i];
            int j = i - 1;
            while (item > Integer.parseInt(map.get(a[j]) + "")) {
                a[j + 1] = a[j];
                if (j >= 1) {
                    j = j - 1;
                } else {
                    j = j - 1;
                    break;
                }

            }
            a[j + 1] = bb;

        }
        return map;
    }

    //消除标点符号
    public static String delectChar(String msg) {
        msg = msg.replace(",", "");
        msg = msg.replace(".", "");
        msg = msg.replace("!", "");
        msg = msg.replace("?", "");
        msg = msg.replace(";", "");
        return msg;
    }

    //读取文本内容
    public static String getFromTxt(String filePath) throws IOException {
        File file = new File(filePath);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String result = "";
        String data = null;
        while ((data = br.readLine()) != null) {
            result += data;
        }
        return result;

    }
}


